package com.group3.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group3.common.result.ApiResponse;
import com.group3.entity.LiveMessage;
import com.group3.entity.LiveRoom;
import com.group3.service.LiveRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/live")
public class LiveRoomController {

    @Autowired
    private LiveRoomService liveRoomService;

    // 创建直播间
    @PostMapping
    public ApiResponse<LiveRoom> createLiveRoom(@RequestBody LiveRoom liveRoom) {
        return ApiResponse.success(liveRoomService.createLiveRoom(liveRoom));
    }

    // 更新直播间
    @PutMapping("/{id}")
    public ApiResponse<LiveRoom> updateLiveRoom(@PathVariable Long id, @RequestBody LiveRoom liveRoom) {
        liveRoom.setId(id);
        return ApiResponse.success(liveRoomService.updateLiveRoom(liveRoom));
    }

    // 删除直播间
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteLiveRoom(@PathVariable Long id) {
        liveRoomService.deleteLiveRoom(id);
        return ApiResponse.success();
    }

    // 获取直播间详情
    @GetMapping("/{id}")
    public ApiResponse<LiveRoom> getLiveRoom(@PathVariable Long id) {
        return ApiResponse.success(liveRoomService.getLiveRoomById(id));
    }

    // 分页获取直播间列表
    @GetMapping
    public ApiResponse<Page<LiveRoom>> listLiveRooms(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return ApiResponse.success(liveRoomService.listLiveRooms(page, size));
    }

    // 获取推荐直播间
    @GetMapping("/recommend/{userId}")
    public ApiResponse<List<LiveRoom>> getRecommendedRooms(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "5") int limit) {
        return ApiResponse.success(liveRoomService.getRecommendedRooms(userId, limit));
    }

    // 开始直播
    @PostMapping("/{id}/start")
    public ApiResponse<Void> startLive(@PathVariable Long id) {
        liveRoomService.startLive(id);
        return ApiResponse.success();
    }

    // 暂停直播
    @PostMapping("/{id}/pause")
    public ApiResponse<Void> pauseLive(@PathVariable Long id) {
        liveRoomService.pauseLive(id);
        return ApiResponse.success();
    }

    // 结束直播
    @PostMapping("/{id}/end")
    public ApiResponse<Void> endLive(@PathVariable Long id) {
        liveRoomService.endLive(id);
        return ApiResponse.success();
    }

    // 添加商品到直播间
    @PostMapping("/{liveId}/products/{productId}")
    public ApiResponse<Void> addProductToLive(
            @PathVariable Long liveId,
            @PathVariable Long productId) {
        liveRoomService.addProductToLive(liveId, productId);
        return ApiResponse.success();
    }

    // 从直播间移除商品
    @DeleteMapping("/{liveId}/products/{productId}")
    public ApiResponse<Void> removeProductFromLive(
            @PathVariable Long liveId,
            @PathVariable Long productId) {
        liveRoomService.removeProductFromLive(liveId, productId);
        return ApiResponse.success();
    }

    // 获取直播间消息
    @GetMapping("/{liveId}/messages")
    public ApiResponse<List<LiveMessage>> getLiveMessages(@PathVariable Long liveId, @RequestParam(defaultValue = "50") int limit) {
        return ApiResponse.success(liveRoomService.getLiveMessages(liveId, limit));
    }

    // 发送消息
    @PostMapping("/{liveId}/messages")
    public ApiResponse<Void> sendMessage(@PathVariable Long liveId, @RequestBody LiveMessage message) {
        message.setLiveId(liveId);
        liveRoomService.sendMessage(message);
        return ApiResponse.success();
    }
}
